<?php
declare (strict_types=1);

namespace app\admin\model\system;

use app\admin\logic\user\Account;
use app\common\model\BaseModel;

/**
 * 定义系统日志数据模型
 */
class Log extends BaseModel
{
    /**
     * 表名
     *
     * @var string
     */
    protected $name = 'admin_log';

    /**
     * 设置自动写入时间
     *
     * @var boolean
     */
    protected $autoWriteTimestamp = true;

    /**
     * 无需创建时间
     *
     * @var boolean
     */
    protected $updateTime = false;

    /**
     * 时间显示格式
     *
     * @var string
     */
    protected $dateFormat = 'Y-m-d H:i';

    /**
     * 定义表主键
     *
     * @var string
     */
    protected $pk = 'id';

    /**
     * 全局查询范围
     *
     * @var [type]
     */
    protected $globalScope = ['mhm'];

    /**
     * 限制机构条件
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-06-16
     *
     * @param    [type]                         $query [description]
     * @return   [type]                                [description]
     */
    public function scopeMhm($query)
    {
        $query->where('mhm_id', request()->mhm_id);
    }

    /**
     * 操作类型
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-06-16
     *
     * @param string $value [description]
     * @param    [type]                         $data  [description]
     */
    public function setLogTypeAttr($value = '', $data)
    {
        $type = [
            'action'  => 0, // 通用该操作
            'create'  => 1, // 新增
            'update'  => 2, // 修改
            'delete'  => 3, // 删除
            'examine' => 4, // 审核
        ];

        return $type[$value] ?? 0;
    }

    /**
     * 新增数据事件
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-06-16
     *
     * @return   string
     */
    public static function onBeforeInsert($log)
    {
        $log->admin_user_id = request()->admin_user_id ?: 1;
        $log->mhm_id        = $log->getAttr('mhm_id') ?: 0;
        if (!$log->mhm_id) {
            $log->mhm_id = request()->user ? request()->user->getAttr('mhm_id') : 1;
        }

        $log->ip = request()->ip();
    }

    /**
     * 日志操作的管理员
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-06-18
     *
     * @return   [type]                         [description]
     */
    public function user()
    {
        return $this->belongsTo(Account::class, 'admin_user_id');
    }

    /**
     * 获取操作类型
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-06-18
     *
     * @param    [type]                         $value [description]
     * @param    [type]                         $data  [description]
     * @return   [type]                                [description]
     */
    public function getLogTypeTextAttr($value, $data)
    {
        $text = [
            0 => '普通操作',
            1 => '新建',
            2 => '修改',
            3 => '删除',
            4 => '审核',
        ];

        return $text[$data['log_type']] ?? '未知';
    }

    /**
     * 按时间搜索
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-06-18
     *
     * @param    [type]                         $query [description]
     * @param    [type]                         $value [description]
     * @param    [type]                         $data  [description]
     * @return   [type]                                [description]
     */
    public function searchCreateTimeAttr($query, $value, $data)
    {
        $query->whereBetweenTime('create_time', strtotime($value[0]), strtotime($value[1]));
    }
}
